<?php
/**
 * Created by PhpStorm.
 * User: fusha_000
 * Date: 9-24/0024
 * Time: 11:43
 */

define('ROOT_PATH','/var/www/html/new/');
define('FX_PATH', ROOT_PATH.'d/class/');

include FX_PATH . 'Log.class.php';

if(!class_exists('go_sql')) {
    include ROOT_PATH.'conf/le.je';
    $le = new go_sql();
    $le->connect();
}
$object = new OrderOwnerList($le);
$ret  = $object->OrderOwnerName(94, 1);
$ret2 = $object->OrderOwnerName(94, 0);
print_r($ret);
print_r($ret2);

class OrderOwnerList {

    private $db;
    const APPLYER=0;
    const BUYER = 1;
    private $where = '';

    public function __construct($db)
    {
        $this->db = $db;
    }
    /**
     * 生成订单查找需要的供应商或分销商数据
     *
     * @author Guangpeng Chen
     * @date   2014年9月24日
     *
     * @param $cacheData string  静态数据文件
     * @return array
     */
    private function _OrderOwnerName($cacheData)
    {
        $sql = 'SELECT m.id,m.dname,getPY(m.dname,1) AS py FROM pft_member m'
              .' LEFT JOIN pft_member_relationship r'
              . $this->where
              .' AND r.ship_type =0 AND r.status=0 ORDER py ASC';
        $this->db->query($sql);
        $rows = array();
        while($row=$this->db->fetch_assoc()) {
            $rows[$row['id']] = $row['py'] .'-'. $row['dname'];
        }
        $saveStr = "<?php\nreturn " . var_export($rows, true)."\n?>";
        $logPath = ROOT_PATH . 'd/logs/';
        if (!$handle = fopen($cacheData, 'w')) {
            \pft\Log\Log::writeLog( date('Y-m-d H:i:s') . "|不能打开文件 $cacheData", $logPath .date('m').'_order_owner_name.err');
            exit;
        }
        if (fwrite($handle, $saveStr) === FALSE) {
            \pft\Log\Log::writeLog( date('Y-m-d H:i:s') . "|不能写入到文件 $cacheData", $logPath .date('m').'_order_owner_name.err');
            exit;
        }
        \pft\Log\Log::writeLog( date('Y-m-d H:i:s') ."|会员静态数据生成成功", $logPath .date('m').'_log_order_owner_name.log');
        return $rows;
    }
    public function OrderOwnerName($id, $type)
    {
        $dataPath = ROOT_PATH . 'd/module/data/order_owner/';
        if ($type==self::APPLYER) {
            $cacheData = $dataPath . "buyer_of_{$id}.cache.php";
            $this->where = <<<SQL
on m.id=r.son_id where r.parent_id={$id}
SQL;
        }
        elseif ($type==self::BUYER) {
            $cacheData = $dataPath . "apply_of_{$id}.cache.php";
            $this->where = <<<SQL
on m.id=r.parent_id where r.son_id={$id} and r.ship_type =0 AND r.status=0
SQL;

        }
        if (file_exists($cacheData) && date('Ymd',filemtime($cacheData))==date('Ymd')) {
            return include $cacheData;
        }
        return $this->_OrderOwnerName($cacheData);
    }
} 